package cn.iocoder.yudao.module.pay.statistics.dal.mysql;

import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.module.pay.statistics.bo.WalletRechargeSummaryRespBO;
import cn.iocoder.yudao.module.pay.statistics.bo.WalletSummaryRespBO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;

/**
 * 支付钱包的统计 Mapper
 *
 * @author owen
 */
@Mapper
@SuppressWarnings("rawtypes")
public interface PayWalletStatisticsMapper extends CoreMapper {

    @Select("""
                    SELECT COUNT(1) AS rechargePayCount, SUM(pay_price) AS rechargePayPrice
                    FROM pay_wallet_recharge
                    WHERE payed = #{payStatus} AND pay_time BETWEEN #{beginTime} AND #{endTime} AND deleted = FALSE
            """)
    WalletSummaryRespBO selectRechargeSummaryByPayTimeBetween(@Param("beginTime") LocalDateTime beginTime,
                                                              @Param("endTime") LocalDateTime endTime,
                                                              @Param("payStatus") Boolean payStatus);

    @Select("""
            SELECT COUNT(1) AS rechargeRefundCount, SUM(pay_price) AS rechargeRefundPrice
            FROM pay_wallet_recharge
            WHERE refunded = #{refundStatus} AND refund_time BETWEEN #{beginTime} AND #{endTime} AND deleted = FALSE
            """)
    WalletSummaryRespBO selectRechargeSummaryByRefundTimeBetween(@Param("beginTime") LocalDateTime beginTime,
                                                                 @Param("endTime") LocalDateTime endTime,
                                                                 @Param("refundStatus") Integer refundStatus);
    @Select("""
            SELECT SUM(price)
            FROM pay_wallet_transaction
            WHERE biz_type = #{bizType} AND create_time BETWEEN #{beginTime} AND #{endTime} AND deleted = FALSE
            """)
    Integer selectPriceSummaryByBizTypeAndCreateTimeBetween(@Param("beginTime") LocalDateTime beginTime,
                                                            @Param("endTime") LocalDateTime endTime,
                                                            @Param("bizType") Integer bizType);
    @Select("""
            <script>
                    SELECT COUNT(DISTINCT wallet_id) AS rechargeUserCount, SUM(pay_price) AS rechargePrice
                    FROM pay_wallet_recharge
                    WHERE payed = #{payStatus}
                    <if test="beginTime != null">
                        AND pay_time <![CDATA[ >= ]]> #{beginTime}
                    </if>
                    <if test="endTime != null">
                        AND pay_time <![CDATA[ <= ]]> #{endTime}
                    </if>
                        AND deleted = FALSE
            </script>
            """)
    WalletRechargeSummaryRespBO selectRechargeSummaryGroupByWalletId(@Param("beginTime") LocalDateTime beginTime,
                                                                     @Param("endTime") LocalDateTime endTime,
                                                                     @Param("payStatus") Boolean payStatus);
    @Select("""
            SELECT IFNULL(SUM(pay_price), 0)
            FROM pay_wallet_recharge
            WHERE payed = #{payStatus} AND deleted = FALSE
            """)
    Integer selectRechargePriceSummary(@Param("payStatus") Boolean payStatus);

}
